//
//  YZFetterParseModel.swift
//  YZFetter
//
//  Created by 康凯 on 2017/7/13.
//  Copyright © 2017年 康凯. All rights reserved.
//

import UIKit
import PKHUD

struct  YZFetterParseModel {
    
    /*
     ****************************************************
     ****  device parameter configuration, total 24 bytes
     ****************************************************
     ****    byte0~1: bluetooth pairing password
     ****    byte2: cellular type
     ****    byte3~8: phone number
     ****    byte9~14: sms center
     ****    byte15~18: server ipaddress
     ****    byte19~20: server port
     ****    byte21: wakeup period time in hours
     ****************************************************
     */
    static func decodeCharaFFF1(for charaData: Data) -> ([String], [String]) {
        guard charaData.count == 20 else {
            HUD.flash(.labeledError(title: "错误", subtitle: "不规范的数据"))
            return ([], [])
        }
        
        var deviceInfo: [String] = []
    
        // IP 地址
        deviceInfo.append(String.init(format: "%d.%d.%d.%d", charaData[0], charaData[1], charaData[2], charaData[3]))
        
        // 端口
        deviceInfo.append(String.init(format: "%d", Int(charaData[5]) * 256 + Int(charaData[4])))
        
        // 告警号码  SMS
        deviceInfo.append(String.init(format: "%02d%02d%02d%02d%02d%d", charaData[6], charaData[7], charaData[8], charaData[9], charaData[10], charaData[11]))
        
        // 状态信息  通信模式
        switch charaData[12] {
        case 1:
            deviceInfo.append("流量模式")
        case 2 :
            deviceInfo.append("短信模式")
        case 3:
            deviceInfo.append("混合模式")
        default:
            break
        }
        
        // 自动唤醒时长
        deviceInfo.append(String.init(format: "%d", charaData[13]))
        
        // 卡号
        deviceInfo.insert((String.init(format: "%02d%02d%02d%02d%02d%d", charaData[14], charaData[15], charaData[16], charaData[17], charaData[18], charaData[19])), at: 0)
        
        return (["卡号",
                 "IP地址",
                 "端口号",
                 "告警号码",
                 "通信模式",
                 "自动唤醒时长"], deviceInfo)
    }
    
    
    /*
     ****************************************************
     ****  device status message
     ****************************************************
     ****    byte0~5: MAC address
     ****    byte6: cellular type
     ****    byte7~13: date time
     ****    byte14: auto wakeup period in hours
     ****    byte15~16: battery voltage
     ****    byte17: cellular module signal
     ****    byte18: sensor status
     ****    byte19: error code
     ****************************************************
     */
    static func decodeCharaFFF5(for charaData: Data) -> ([String], [String]) {
        
        guard charaData.count == 20 else {
            HUD.flash(.labeledError(title: "错误", subtitle: "不规范的数据"))
            return ([], [])
        }
        
        var statusInfo: [String] = []
        
        // Mac地址
        statusInfo.append(String(format: "%0X%0X%0X%0X%0X%0X", charaData[0], charaData[1], charaData[2], charaData[3], charaData[4], charaData[5]))
        
        // 控制单元类型
        statusInfo.append(String.init(format: "%c", charaData[6]))
        
        // 时间
        statusInfo.append(String.init(format: "%02d%02d.%02d.%02d %02d:%02d:%02d", charaData[7], charaData[8], charaData[9], charaData[10], charaData[11], charaData[12], charaData[13]))
        
        // 自动唤醒间隔时长
        statusInfo.append(String.init(format: "%d", charaData[14]))
        
        // 电池电压
        statusInfo.append(String.init(format: "%d.%02dV", charaData[15] % 10, charaData[16] % 100))
        
        // 小时无线网络信号强度
        statusInfo.append(String.init(format: "%02d", charaData[17]))
        
        // 锁舌状态
        let fetterStatus = Int(charaData[18] & 0x01)    // SX
        let fetterStatusMsg = fetterStatus == 0 ? "关闭" : "开启"
        statusInfo.append(String.init(format: "\(fetterStatus) \(fetterStatusMsg)"))
        
        // 手动唤醒状态
        statusInfo.append(String.init(format: "%d%d", (charaData[18] >> 1) & 0x01,
                                                      (charaData[18] >> 2) & 0x01))
        
        // 震动唤醒状态
        statusInfo.append(String.init(format: "%d%d", (charaData[18] >> 3) & 0x01,
                                                      (charaData[18] >> 4) & 0x01))
        
        // 锁销状态
        let TL1 = Int((charaData[18] >> 5) & 0x01)       // TL1 0为关闭状态
        let TL2  = Int((charaData[18] >> 6) & 0x01)      // TL2 0为开启状态
        if TL1 == TL2 {
            statusInfo.append("1/1 异常")
        }else if (TL2 == 0) {
            statusInfo.append("\(TL1)/\(TL2) 开启")
        }else if (TL1 == 0) {
            statusInfo.append("\(TL1)/\(TL2) 关闭")
        }
        
        
        // 挂故障码
        statusInfo.append(String.init(format: "%d", charaData[19]))
        
        return (["MAC地址",
                 "G/C",
                 "时间",
                 "自动唤醒时长",
                 "电压",
                 "无线网络信号",
                 "锁舌状态",
                 "手动唤醒状态",
                 "震动唤醒状态",
                 "锁销状态",
                 "故障代码"], statusInfo)
    }
}
